/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.maixiami;

import com.taobao.api.ApiException;
import com.taobao.api.TaobaoClient;
import com.taobao.api.domain.ShopCat;
import com.taobao.api.domain.TaobaokeShop;
import com.taobao.api.request.TaobaokeShopsGetRequest;
import com.taobao.api.response.TaobaokeShopsGetResponse;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.List;
import org.apache.log4j.Logger;

/**
 *
 * @author qye
 */
public class ShopCrawler {
    static Logger logger = Logger.getLogger(ShopCrawler.class);
    
    static final String[] LEVELS = new String[] {
        "1heart", "2heart", "3heart", "4heart", "5heart", 
        "1diamond", "2diamond", "3diamond", "4diamond", "5diamond", 
        "1crown", "2crown", "3crown", "4crown", "5crown", 
        "1goldencrown", "2goldencrown", "3goldencrown", "4goldencrown", "5goldencrown"
    };
    
    List<ShopCat> shopCats;
    int fetchInterval = 0;
    public ShopCrawler(List<ShopCat> shopCats, int fetchInterval) {
        this.shopCats = shopCats;
        this.fetchInterval = fetchInterval;
    }
    
    public void crawl(String outfile) throws FileNotFoundException, IOException, InterruptedException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(outfile));
        
        TaobaokeShopsGetRequest req = new TaobaokeShopsGetRequest();
        req.setFields("user_id, click_url, shop_title, commission_rate, seller_credit, shop_type, auction_count, total_auction");
        req.setNick(App.user);
        for(ShopCat cat : shopCats) {
            req.setCid(cat.getCid());
            logger.info(cat.getCid());
            for(int i = 0; i < LEVELS.length - 1; i++) {
                req.setStartCredit(LEVELS[i]);
                req.setEndCredit(LEVELS[i + 1]);
                logger.info(LEVELS[i]);
                for(long page = 1; page <= 99; page++) {
                    try {
                        req.setPageNo(page);
                        TaobaoClient client = App.getTaobaoClient();
                        TaobaokeShopsGetResponse res = client.execute(req);
                        if(res.isSuccess()) {
                            
                            if(res.getTaobaokeShops() == null)
                                // no extra pages, break it
                                break;
                            for(TaobaokeShop shop : res.getTaobaokeShops())
                                oos.writeObject(shop);
                        }else {
                            logger.warn(res.getMsg());
                            logger.error(res.getErrorCode());
                        }
                    }catch(ApiException ex) {
                        logger.error(ex);
                    }
                    
                    Thread.sleep(fetchInterval);
                }
            }// end of for i
        }
        
        oos.close();
    }
}
